package com.langyiquan.service.impl;

import com.langyiquan.bo.SysUserBO;
import com.langyiquan.bo.SysUserInfoBO;
import com.langyiquan.exception.http.VerifyException;
import com.langyiquan.mapper.SysUserMapper;
import com.langyiquan.pojo.SysUser;
import com.langyiquan.service.SysUserService;
import com.langyiquan.utils.Md5Util;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.security.NoSuchAlgorithmException;
import java.util.Date;

@Service
public class SysUserServiceImpl implements SysUserService {

    private static final String SYS_USER_AVATAR = "https://uufe-web.oss-cn-beijing.aliyuncs.com/PicLib/uunote/userphoto/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20190926150616_1569481588771.jpg";

    @Autowired
    private SysUserMapper sysUserMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public SysUser querySysUserInfo(Integer sysUserId) {
        SysUser sysUser = sysUserMapper.selectByPrimaryKey(sysUserId);
        sysUser.setPassword(null);
        return sysUser;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public SysUser querySysUserForLogin(SysUserBO sysUserBO) {

        Example example = new Example(SysUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username",sysUserBO.getUsername());
        try {
            criteria.andEqualTo("password", Md5Util.getMd5Str(sysUserBO.getPassword()));
        } catch (NoSuchAlgorithmException e) {
            throw new VerifyException(1003);
        }
        SysUser sysUser = sysUserMapper.selectOneByExample(example);

        return sysUser;

    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean queryUsernameIsExist(String username) {
        Example example = new Example(SysUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username",username);
        SysUser sysUser = sysUserMapper.selectOneByExample(example);
        return sysUser == null ? false : true;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public SysUser createSysUser(SysUserInfoBO sysUserInfoBO) {
        SysUser sysUser = new SysUser();
        sysUser.setUsername(sysUserInfoBO.getUsername());
        try {
            sysUser.setPassword(Md5Util.getMd5Str(sysUserInfoBO.getPassword()));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        sysUser.setRealname(sysUserInfoBO.getRealname());
        sysUser.setMobile(sysUserInfoBO.getMobile());
        sysUser.setDeptId(sysUserInfoBO.getDeptId());
        sysUser.setAvatar(SYS_USER_AVATAR);
        sysUser.setStatus(sysUserInfoBO.getStatus());
        sysUser.setCreatedTime(new Date());
        sysUser.setUpdatedTime(new Date());
        sysUser.setOperatorIp(sysUserInfoBO.getOperatorIp());
        sysUser.setOperatorId(sysUserInfoBO.getOperatorId());
        sysUserMapper.insert(sysUser);
        return sysUser;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public SysUser updateSysUserInfo(Integer sysUserId, SysUserInfoBO sysUserInfoBO) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserInfoBO,sysUser);
        sysUser.setId(sysUserId);
        sysUser.setPassword(null);
        sysUserMapper.updateByPrimaryKeySelective(sysUser);
        return querySysUserInfo(sysUserId);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updateSysUserFace(Integer sysUserId, String avatarUrl) {
        SysUser sysUser = new SysUser();
        sysUser.setId(sysUserId);
        sysUser.setAvatar(avatarUrl);
        sysUserMapper.updateByPrimaryKeySelective(sysUser);
    }
}
